﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Core.Domain;
using KL.Store.Web.Models;
using KL.Store.Data;
using KL.Store.Core;


namespace KL.Store.Web.Controllers
{
    [RequireHttps]
    [Authorize]
    public class OrderController : KL.Store.Core.Web.BaseController
    {
        #region Index

        public ActionResult Index()
        {
          return View();
        }

        #endregion

        #region Get

        public ActionResult Get()
        {
            OrderModel orderModel = new OrderModel();
            orderModel.Orders = dbOrder.GetList(eStoreUser.AccountId);
            if (orderModel.Orders == null)
            {
                orderModel.Orders = new List<Order>();
            }
            return PartialView("Get", orderModel);
        }

        #endregion

        #region Cancel

        public ActionResult Cancel(int? id)
        {
            
            if (eStoreUser.AccountId > 0 && id.HasValue && (int)id > 0)
            {
                Order order = dbOrder.Get(eStoreUser.AccountId, (int)id);

                int ret = -1;
                if (order != null && order.CanBeCancelled)
                {
                    ret = dbOrder.Cancel(eStoreUser.AccountId, (int)id);
                }

                return RedirectToAction("Index","Order");
            }
            else
            {
                return Content("İşleminiz gerçekleştirilemedi.");
            }
        }

        #endregion

        #region Detail

        public ActionResult Detail()
        {

            return View();
        }

        #endregion

        #region GetDetail

        public ActionResult GetDetail(int? id)
        {

            OrderDetailModel orderDetailModel = new OrderDetailModel();
            if (id.HasValue && eStoreUser.AccountId > 0)
            {
                orderDetailModel.Order = dbOrder.Get(eStoreUser.AccountId, (int)id);

                if (orderDetailModel.Order != null)
                {
                    orderDetailModel.OrderDetails = dbOrder.GetDetail(eStoreUser.AccountId, (int)id);
                    if (orderDetailModel.OrderDetails == null)
                    {
                        orderDetailModel.OrderDetails = new List<OrderDetail>();
                    }

                    #region Prepare Cargo Urls

                    if (!string.IsNullOrEmpty(orderDetailModel.Order.CargoReportUrl))
                    {
                        orderDetailModel.OrderCargoRefs = dbOrder.GetCargoRefList(eStoreUser.AccountId, (int)id);
                        foreach (OrderCargo oc in orderDetailModel.OrderCargoRefs)
                        {
                            if (!string.IsNullOrEmpty(oc.ProductCargoRef))
                            {
                                oc.LinkProduct = orderDetailModel.Order.CargoReportUrl.Replace("{Ref}", oc.ProductCargoRef)
                                                                                      .Replace("{CargoDate}",oc.ProductCargoDate);
                            }
                            if (!string.IsNullOrEmpty(oc.InvoiceCargoRef))
                            {
                                oc.LinkInvoice = orderDetailModel.Order.CargoReportUrl.Replace("{Ref}", oc.InvoiceCargoRef)
                                                                                      .Replace("{CargoDate}", oc.InvoiceCargoDate);
                            }
                        }
                    }

                    #endregion
                }
                else
                {
                    orderDetailModel.Order = new Order();
                    orderDetailModel.OrderDetails = new List<OrderDetail>();
                }
                return PartialView("GetDetail", orderDetailModel);
            }
            else
            {
                return Content("Sipariş Detayı Bulunamadı.");
            }
        }

        #endregion
    }
}
